<template>
  <div class="app-container">
    <!-- 条件查询 v-permission="'user:search'"  -->
    <el-card class="box-card">
      <el-form :inline="true" :model="query" size="mini">
        <el-form-item label="文章标题/用户名" width="120px;">
          <el-input v-model.trim="query.title" clearable placeholder="请输入文章标题"/>
        </el-form-item>
        <el-form-item label="举报人ID">
          <el-input v-model.trim="query.userId" clearable placeholder="请输入举报人ID"/>
        </el-form-item>
        <el-form-item label="被举报用户ID">
          <el-input v-model.trim="query.entityUserId" clearable placeholder="请输入被举报用户ID"/>
        </el-form-item>
        <el-form-item label="举报的实体">
          <!-- 举报的实体：(0 文章；1 留言；2 回复；3 私信 ) -->
          <el-select v-model="query.entityType" clearable style="width: 120px">
            <el-option label="文章" :value="0"/>
            <el-option label="留言" :value="1"/>
            <el-option label="回复" :value="2"/>
            <el-option label="私信" :value="3"/>
          </el-select>
        </el-form-item>
        <el-form-item label="举报目标ID">
          <el-input v-model.trim="query.entityId" clearable placeholder="请输入举报的实体ID"/>
        </el-form-item>
        <el-form-item label="举报内容">
          <el-input v-model.trim="query.content" clearable/>
        </el-form-item>
        <el-form-item label="举报原因">
          <!-- 举报原因：(0 作品侵权；1 涉嫌违法；2 内容不雅；3 骚扰谩骂；4 虚假宣传；5 低质灌水；6 其它 ) -->
          <el-select v-model="query.reportType" clearable style="width: 120px">
            <el-option label="作品侵权" :value="0"/>
            <el-option label="涉嫌违法" :value="1"/>
            <el-option label="内容不雅" :value="2"/>
            <el-option label="骚扰谩骂" :value="3"/>
            <el-option label="虚假宣传" :value="4"/>
            <el-option label="低质灌水" :value="5"/>
            <el-option label="其它" :value="6"/>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <!-- 状态：( 0 待处理；1 已处理 ) -->
          <el-select v-model="query.status" clearable style="width: 120px">
            <el-option label="待处理" :value="0"/>
            <el-option label="已处理" :value="1"/>
          </el-select>
        </el-form-item>
        <el-form-item label="日期范围">
          <el-date-picker
            v-model="query.begin"
            clearable
            placeholder="开始时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="-">
          <el-date-picker
            v-model="query.end"
            clearable
            placeholder="结束时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="补充说明">
          <el-input v-model.trim="query.description" clearable/>
        </el-form-item>
        <el-form-item label="处理结果说明">
          <el-input v-model.trim="query.result" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button v-permission="'report:search'" icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          <el-button v-permission="'report:search'" icon="el-icon-refresh" @click="reload">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格显示列表数据 -->
    <el-table
      :data="list"
      stripe
      border
      height="650"
    >
      <el-table-column label="序号" width="70" fixed align="center">
        <template slot-scope="scope">
          <!--  page.current 当前页码；page.size 每页记录数 -->
          {{ (current - 1) * size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="userId" label="举报人ID" align="center" width="200"/>
      <el-table-column prop="entityUserId" label="被举报用户ID" align="center" width="200"/>
      <!-- 举报的实体：(0 帖子；1 评论；2 回复；3 私信 ) -->
      <el-table-column align="center" prop="entityType" label="举报实体" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.entityType == 0" type="success">文章</el-tag>
          <el-tag v-if="scope.row.entityType == 1" type="primary">留言</el-tag>
          <el-tag v-if="scope.row.entityType == 2" type="info">回复</el-tag>
          <el-tag v-if="scope.row.entityType == 3" type="warning">私信</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="entityId" label="被举报实体ID" align="center" width="200"/>
      <!-- 状态：( 0 待处理；1 已处理 ) -->
      <el-table-column align="center" prop="status" label="状态" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="" effect="dark">待处理</el-tag>
          <el-tag v-if="scope.row.status == 1">已处理</el-tag>
        </template>
      </el-table-column>
      <!-- 举报原因：(0 作品侵权；1 涉嫌违法；2 内容不雅；3 骚扰谩骂；4 虚假宣传；5 低质灌水；6 其它 )' -->
      <el-table-column align="center" prop="reportType" label="举报原因" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.reportType == 0" type="">作品侵权</el-tag>
          <el-tag v-if="scope.row.reportType == 1" type="danger">涉嫌违法</el-tag>
          <el-tag v-if="scope.row.reportType == 2" type="danger">内容不雅</el-tag>
          <el-tag v-if="scope.row.reportType == 3" type="danger">骚扰谩骂</el-tag>
          <el-tag v-if="scope.row.reportType == 4" type="danger">虚假宣传</el-tag>
          <el-tag v-if="scope.row.reportType == 5" type="danger">低质灌水</el-tag>
          <el-tag v-if="scope.row.reportType == 6" type="danger">其它</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="举报补充说明" width="300" align="center"/>
      <el-table-column prop="title" label="文章标题 / 用户名" align="center" width="300"/>
      <el-table-column prop="content" label="举报的内容" width="400" align="center"/>
      <el-table-column prop="result" label="处理结果说明" width="300" align="center"/>
      <el-table-column align="center" prop="updateTime" label="更新时间" width="160px"/>
      <el-table-column align="center" prop="createTime" label="创建时间" width="160px"/>
      <el-table-column prop="id" label="ID" align="center" width="200"/>
      <el-table-column align="center" label="操作" min-width="200px" fixed="right">
        <template slot-scope="scope">
          <el-button v-permission="'report:audit'" type="primary" size="mini" @click="handleEdit(scope.row.id)">审查处理
          </el-button>
          <!-- 根据ID删除留言 v-permission="'article:delete'" -->
          <el-button v-permission="'report:audit'" type="danger" size="mini" @click="handleDelete(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      align="center"
      background
      :current-page="current"
      :page-sizes="[10, 20,30,50,100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 审核举报子组件 -->
    <edit :report="report" :dialog-visible="dialogVisible" @handleClose="handleClose" @fetchData="fetchData"/>

  </div>
</template>
<script>
import api from '@/api/report'
import Edit from './edit'

export default {
  name: 'Report',
  components: {
    Edit
  },
  data() {
    return {
      list: [],
      current: 1,
      size: 10,
      total: 0,
      query: {},
      // 审核处理举报
      report: {},
      dialogVisible: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取条件分页列表数据
    async fetchData() {
      const { data } = await api.search(this.current, this.size, this.query)
      this.list = data.records
      this.total = Number(data.total)
    },
    // 切换每页显示多少条记录
    handleSizeChange(size) {
      this.size = size
      this.fetchData()
    },
    // 切换当前页码
    handleCurrentChange(current) {
      this.current = current
      this.fetchData()
    },
    // 条件查询
    queryData() {
      this.current = 1
      this.fetchData()
    },
    // 条件查询-重置表单
    reload() {
      this.query = {}
      this.fetchData()
    },
    // 打开编辑窗口
    async handleEdit(id) {
      const { data } = await api.getById(id)
      this.report = data.report
      this.dialogVisible = true
    },
    // 关闭审核处理对话框
    handleClose() {
      this.dialogVisible = false
    },
    // 根据ID删除
    handleDelete(id) {
      this.$confirm('确定要删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送请求
        api.deleteById(id).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message.success(response.message)
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
<style scoped>
.el-pagination {
  margin: 20px 0 50px;
}
</style>
